<?php
/**
 * Created by 橙橙同学.
 * User: 橙橙同学
 * Date: 2017/11/13
 * Time: 下午10:49
 */
namespace app;
/**
 * Class ParkTemplateMessage
 * @package app
 * 会员模板消息
 */
class MemberTemplateMessager{

    const M_SCORE = 'fcZXMj1Do2G-pqU1lZm1hSmk2Ssqrt_5pW7GfGRyPWs'; // 会员积分消费提醒
    const O_ORDER = 'bbDq7mbwY9s3EP1ZBVPZ3j-db1AbtHuPlkjtfeKouoI'; // 消费成功通知
    const M_REG   = 'EZSxe942B3rQrERPnPuIjsIFI3BX2eA5mAipoEwbd3Y'; // 成为会员通知
    const S_CONSUME = 'HIX5LheyX9pWfU_ET29Bhf4NjT_dN_vb8pSiDuAwzO4'; // 会员积分变动提醒

    
    /**
     * 注册成为会员
     * @param $openid
     * @param $json
     * @return string
     */
    public static function Reg($openid, $json)
    {
        $msg = "客服电话： 029-86300000\n\n";
        $data = array(
            'touser'      => $openid,
            'template_id' => self::M_REG,
            'url'         => '',
            'topcolor'    => '#69008C',
            'data'        => array(
                'first'    => array(
                    'value' => "尊敬的会员 {$json['name']}，恭喜您成功加入赛格国际购物中心会员！\n",
                    'color' => '#000000'
                ),
                'keyword1' => array(
                    'value' => $json['cardNumber'],
                    'color' => '#69008C'
                ),
                'keyword2' => array(
                    'value' => $json['date'] . "\n",
                    'color' => '#69008C'
                ),
                'remark'   => array(
                    'value' => $msg,
                    'color' => '#000000'
                ),
            )
        );
        return json_encode($data);
    }

    /**
     * 会员积分消费提醒
     *
     * @param $openid
     * @param $json
     *
     * @return mixed
     */
    private static function score($openid, $json)
    {
        $data = array(
            'touser'      => $openid['openid'],
            'template_id' => self::M_SCORE,
            'url'         => '',
            'topcolor'    => '#69008C',
            'data'        => array(
                'first'    => array(
                    'value' => "尊敬的会员 {$json['name']}您好，您于{$json['date']}在赛格国际购物中心消费积分。积分使用详情如下：\n",
                    'color' => '#000000'
                ),
                'XM'       => array(
                    'value' => $json['name'],
                    'color' => '#69008C'
                ),
                'KH'       => array(
                    'value' => $json['cardNumber'],
                    'color' => '#69008C'
                ),
                'CONTENTS' => array(
                    'value' => "{$json['content']}。您本次积分{$json['score']}分,现可用积分为{$json['totalScore']}分。\n",
                    'color' => '#69008C'
                ),
                'remark'   => array(
                    'value' => "祝您购物愉快！如有疑问，请拨打服务热线 029-86300000",
                    'color' => '#000000'
                ),
            )
        );
        return json_encode($data);
    }

    /**
     * 会员消费模板
     *
     * @param $openid
     * @param $json
     *
     * @return string
     */
    private static function order($openid, $json)
    {
        $msg = "客服电话： 029-86300000\n\n";
        $data = array(
            'touser'      => $openid['openid'],
            'template_id' => self::O_ORDER,
            'url'         => '',
            'topcolor'    => '#69008C',
            'data'        => array(
                'first'  => array(
                    'value' => "尊敬的会员 {$json['name']}您好，您本次在赛格国际购物中心的消费详情如下：\n",
                    'color' => '#000000'
                ),
                'time'   => array(
                    'value' => $json['date'],
                    'color' => '#69008C'
                ),
                'org'    => array(
                    'value' => $json['shoppeName'],
                    'color' => '#69008C'
                ),
                'type'   => array(
                    'value' => $json['type'],
                    'color' => '#69008C'
                ),
                'money'  => array(
                    'value' => round($json['amount'],2) . '元',
                    'color' => '#69008C'
                ),
                'point'  => array(
                    'value' => $json['score'] . "分\n",
                    'color' => '#69008C'
                ),
                'remark' => array(
                    'value' => $msg,
                    'color' => '#000000'
                ),
            )
        );
        return json_encode($data);
    }


    public static function templateMessage($cardNum, $type, $json)
    {
        //根据车牌号码获取用户的openid
        $openid = self::getOpenidByCardNum($cardNum);
        // 根据消息类型返回相关字符串
        $typeFunc = '';
        switch ($type) {
            // 积分消费
            case 'M_SCORE':
                $jsondata = json_decode($json, true);
                $status   = Check::checkScore($jsondata);

                if ($status !== true) {
                    return json('0', $status);
                }
                $typeFunc = self::score($openid, $jsondata);
                break;
            // 会员消费
            case 'O_ORDER':
                $jsondata = json_decode($json, true);
                $status   = Check::checkOrder($jsondata);

                if ($status !== true) {
                    return json('0', $status);
                }
                $typeFunc = self::order($openid, $jsondata);
                break;
            // 成为会员
            case 'M_REG':
                $jsondata = json_decode($json, true);
                $status   = Check::checkReg($jsondata);
                if ($status !== true) {
                    return json('0', $status);
                }
                $typeFunc = self::Reg($openid['openid'], $jsondata);
                break;
            default:
                return json('0', '类型不存在');
        }

        return $typeFunc;

    }
}